﻿using System;
using Microsoft.BizTalk.Component.Interop;

namespace BizTalk.Archiving.PipelineComponents
{
    /// <summary>
    /// IPersistPropertyBag Implementation
    /// </summary>
    public partial class ArchiveComponent : IPersistPropertyBag
    {
        #region Properties
        /// <summary>
        /// Enabled flag for Db Archive
        /// </summary>
        public bool IsArchiveToDb { get; set; }

        /// <summary>
        /// File Extension
        /// </summary>
        public string DbFileExtensions  { get; set; }

        /// <summary>
        /// ConnectionString
        /// </summary>
        public string DbConnStr { get; set; }

        /// <summary>
        /// Provider Name: System.Data.SqlClient
        /// </summary>
        public string DbConnProvider { get; set; }

        /// <summary>
        /// List of message properties that will be included in the xml that will be password to stored procedure
        /// Format is property name;namespace
        /// Ex: FileCreationTime;http://schemas.microsoft.com/BizTalk/2003/file-properties
        /// For multiple properties delimit it by | pipe symbol
        /// </summary>
        public string DbPropList { get; set; }

        /// <summary>
        /// Name of stored procedure for archiving, it should accept Xml, and VarBinaryData
        /// </summary>
        public string DbSPName { get; set; }

        /// <summary>
        /// File Archive Enabled Flag
        /// </summary>
        public bool IsArchiveToFile { get; set; }

        /// <summary>
        /// File Name
        /// </summary>
        public string FileArchiveFileName { get; set; }

        /// <summary>
        /// Backup Folder
        /// </summary>
        public string FileArchiveBackupFolder { get; set; }

        /// <summary>
        /// Zip Password
        /// </summary>
        public string CompressionPassword { get; set; }

        /// <summary>
        /// Is Overwrite file
        /// </summary>
        public bool FileArchiveIsOverwriteFiles { get; set;}

        /// <summary>
        /// 
        /// </summary>
        public bool IsCompressFile { get; set; }

        /// <summary>
        /// Connection User Name
        /// </summary>
        public string FileArchiveUserName { get; set; }

        /// <summary>
        /// Connection Domain
        /// </summary>
        public string FileArchiveUserDomain { get; set; }

        /// <summary>
        /// Connection Password
        /// </summary>
        public string FileArchiveUserPwd { get; set; }

        
        #endregion Properties

        void IPersistPropertyBag.GetClassID(out Guid classID)
        {
            classID = new Guid("c79c9723-7836-4f73-ace8-41acadc8f1a2");
        }

        void IPersistPropertyBag.InitNew()
        {
        }

        void IPersistPropertyBag.Load(IPropertyBag propertyBag, int errorLog)
        {
            object val = null;
            object sval = null;
            
            val = this.ReadPropertyBag(propertyBag, "IsArchiveToDb");
            if ((val != null))
            {
                this.IsArchiveToDb = ((bool)(val));
            }

            sval = this.ReadPropertyBag(propertyBag, "DbConnStr");
            if ((sval != null))
            {
                this.DbConnStr  = ((string)(sval));
            }

            sval = this.ReadPropertyBag(propertyBag, "DbConnProvider");
            if ((sval != null))
            {
                this.DbConnProvider = ((string)(sval));
            }

            sval = this.ReadPropertyBag(propertyBag, "DbPropList");
            if ((sval != null))
            {
                this.DbPropList = ((string)(sval));
            }

            sval = this.ReadPropertyBag(propertyBag, "DbSPName");
            if ((sval != null))
            {
                this.DbSPName = ((string)(sval));
            }

            sval = this.ReadPropertyBag(propertyBag, "CompressionPassword");
            if ((sval != null))
            {
                this.CompressionPassword = ((string)(sval));
            }

            val = this.ReadPropertyBag(propertyBag, "IsArchiveToFile");
            if ((val != null))
            {
                this.IsArchiveToFile = ((bool)(val));
            }

            val = this.ReadPropertyBag(propertyBag, "IsCompressFile");
            if ((val != null))
            {
                this.IsCompressFile = ((bool)(val));
            }

            sval = this.ReadPropertyBag(propertyBag, "FileArchiveFileName");
            if ((sval != null))
            {
                this.FileArchiveFileName = ((string)(sval));
            }

            sval = this.ReadPropertyBag(propertyBag, "FileArchiveBackupFolder");
            if ((sval != null))
            {
                this.FileArchiveBackupFolder = ((string)(sval));
            }


            val = this.ReadPropertyBag(propertyBag, "FileArchiveIsOverwriteFiles");
            if ((val != null))
            {
                this.FileArchiveIsOverwriteFiles = ((bool)(val));
            }

            sval = this.ReadPropertyBag(propertyBag, "FileArchiveUserName");
            if ((sval != null))
            {
                this.FileArchiveUserName = ((string)(sval));
            }

            sval = this.ReadPropertyBag(propertyBag, "FileArchiveUserDomain");
            if ((sval != null))
            {
                this.FileArchiveUserDomain = ((string)(sval));
            }

            sval = this.ReadPropertyBag(propertyBag, "FileArchiveUserPwd");
            if ((sval != null))
            {
                this.FileArchiveUserPwd = ((string)(sval));
            }

            sval = this.ReadPropertyBag(propertyBag, "DbFileExtensions");
            if ((sval != null))
            {
                this.DbFileExtensions = ((string)(sval));
            }

          
        
        }


        void IPersistPropertyBag.Save(IPropertyBag propertyBag, bool clearDirty, bool saveAllProperties)
        {
            WritePropertyBag(propertyBag, "IsArchiveToDb", (object)IsArchiveToDb);
            WritePropertyBag(propertyBag, "DbConnProvider", (object)DbConnProvider);
            WritePropertyBag(propertyBag, "DbConnStr", (object)DbConnStr);
            WritePropertyBag(propertyBag, "DbPropList", (object)DbPropList);
            WritePropertyBag(propertyBag, "DbSPName", (object)DbSPName);

            WritePropertyBag(propertyBag, "CompressionPassword", (object)CompressionPassword);
            WritePropertyBag(propertyBag, "FileArchiveBackupFolder", (object)FileArchiveBackupFolder);
            
            WritePropertyBag(propertyBag, "FileArchiveUserDomain", (object)FileArchiveUserDomain);
            WritePropertyBag(propertyBag, "FileArchiveUserName", (object)FileArchiveUserName);
            WritePropertyBag(propertyBag, "FileArchiveUserPwd", (object)FileArchiveUserPwd);
            WritePropertyBag(propertyBag, "FileArchiveFileName", (object)FileArchiveFileName);

            
            WritePropertyBag(propertyBag, "IsArchiveToFile", (object)IsArchiveToFile);
            WritePropertyBag(propertyBag, "FileArchiveIsOverwriteFiles", (object)FileArchiveIsOverwriteFiles);
            WritePropertyBag(propertyBag, "DbFileExtensions", (object)DbFileExtensions);
            WritePropertyBag(propertyBag, "IsCompressFile", (object)IsCompressFile);
        }
        /// <summary> 
        /// Reads property value from property bag 
        /// </summary> 
        /// <param name="propertyBag">Property bag</param> 
        /// <param name="propName">Name of property</param> 
        /// <returns>Value of the property</returns> 
        private object ReadPropertyBag(Microsoft.BizTalk.Component.Interop.IPropertyBag propertyBag, string propName)
        {
            object val = null;
            try
            {
                propertyBag.Read(propName,
                out val, 0);
            }
            catch (System.ArgumentException)
            {
                return val;
            }
            catch (System.Exception e)
            {
                throw new System.ApplicationException(e.Message);
            }
            return val;
        }
        /// <summary> 
        /// Writes property values into a property bag. 
        /// </summary> 
        /// <param name="propertyBag">Property bag.</param> 
        /// <param name="propName">Name of property.</param> 
        /// <param name="val">Value of property.</param> 
        private void WritePropertyBag(Microsoft.BizTalk.Component.Interop.IPropertyBag propertyBag, string propName, object val)
        {
            try
            {
                propertyBag.Write(propName,
                ref val);
            }
            catch (System.Exception e)
            {
                throw new System.ApplicationException(e.Message);
            }
        }

    }
}
